home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Messaging / OSL / OSLGSExm.c < prev    next >
Encoding:
Text File  |  1996-04-22  |  6.9 KB  |  251 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OSLGSExm.c (Orignal name: OSLGetSetExmn.c)
  3.  
  4.     Contains:    OSL Globals
  5.  
  6.     Owned by:    Nick Pilch
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <7>     6/27/95    NP        1262792: Fix stack dynamic allocation
  13.                                     problem
  14.          <6>     6/24/95    TJ        Changed kNumStaticContextStackEntries from
  15.                                     a const to a #define so it would compile
  16.                                     with SC.
  17.          <5>     6/23/95    NP        1195474: Make Resolve reentrant.
  18.          <4>     2/22/95    eeh        1222904: fix use of SetCurrentContext
  19.          <3>      2/8/95    NP        1218550: Don't allocate OSLContexts
  20.                                     dynamically.
  21.          <2>     8/19/94    NP        1181622: Ownership fix.
  22.          <6>      5/5/94    eeh        bug #1160654: fix for SCPP
  23.          <5>      5/2/94    eeh        bug #1160654: various PPC native changes
  24.          <4>     8/18/93    NP        Added get and set current context.
  25.          <3>     7/29/93    NP        Removed compiler warning for unused
  26.                                     parameters.
  27.          <2>     7/28/93    NP        Mods for new token type, OSLToken.
  28.          <1>     7/21/93    NP        first checked in
  29.  
  30.     To Do:
  31. */
  32.  
  33. // ASSUMPTION: This code will be used in a library that has per-context globals!
  34.  
  35. ///////////////////////////////////////////////////////////////////////////////                                            
  36. //        ©Apple Computer, Inc.  1992         
  37. //              All Rights Reserved.                
  38. //        Author: Eric House
  39. ///////////////////////////////////////////////////////////////////////////////                                            
  40.  
  41.  
  42. #include "OSLPriv.h"
  43.  
  44.  
  45. #define kExmnKey1 'eone'
  46. #define kExmnKey2 'etwo'
  47.  
  48. #define    UNUSED(x) ((void) &x)
  49.  
  50. // this guy should go away, and be replaced by calls to GetAccessor or 
  51. // GetEventHandler using the keys defined above.  This should allow a
  52. // more general storage mechanism if it becomes necessary.
  53. // static AEDesc theGlobal ;
  54.  
  55. OSLToken    theGlobalToken;
  56.  
  57. static OSErr
  58. GetGlobalDesc( unsigned long key1, unsigned long key2, AEDesc* desc )
  59. {
  60. //    return iAEGetObjectAccessor( key1, key2, (accessorProcPtr *)&desc->descriptorType,
  61. //            (long *)&desc->dataHandle, false, 0) ;
  62. //    *desc = theGlobalDesc;
  63.     UNUSED( key1 );
  64.     UNUSED( key2 );
  65.     UNUSED( desc );
  66.     return noErr;
  67. }
  68.  
  69. static OSErr
  70. SetGlobalDesc( unsigned long key1, unsigned long key2, AEDesc* desc )
  71. {
  72. //    return iAEInstallObjectAccessor( key1, key2, (accessorProcPtr)desc->descriptorType,
  73. //            (long)desc->dataHandle, false, 0) ;
  74. //    theGlobalDesc = *desc;
  75.     UNUSED( key1 );
  76.     UNUSED( key2 );
  77.     UNUSED( desc );
  78.     return noErr;
  79. }
  80.  
  81.  
  82. // Get the currently available "global" exmn token
  83. OSErr
  84. GetExmn( OSLToken *result )
  85. {
  86.     *result = theGlobalToken;
  87.     return noErr;
  88. //    return GetGlobalDesc( kExmnKey1, kExmnKey2, result ) ;
  89. }
  90.  
  91. // Set the "global" exmn token to this one
  92. OSErr
  93. SetExmn( OSLToken *newDesc )
  94. {
  95.     theGlobalToken = *newDesc;
  96.     return noErr;
  97. //    return SetGlobalDesc( kExmnKey1, kExmnKey2, newDesc ) ;
  98. }
  99.  
  100.  
  101. // Replace the currently available "global" exmn token with
  102. // the one provided here.  And return the old value in its
  103. // place.
  104. OSErr
  105. SwapExmn( AEDesc *oldAndNewDesc )
  106. {
  107.     AEDesc temp ;
  108.     OSErr err = GetGlobalDesc( kExmnKey1, kExmnKey2, &temp ) ;
  109.     if ( err == noErr )
  110.     {
  111.         err = SetGlobalDesc( kExmnKey1, kExmnKey2, oldAndNewDesc ) ;
  112.         if ( err == noErr )
  113.             *oldAndNewDesc = temp ;
  114.     }
  115.     return err ;
  116. }
  117.  
  118. //==============================================================================
  119. // Context stack
  120. //
  121. //    Maintain stack of current contexts, one for each time OSLResolve is called.
  122. //==============================================================================
  123.  
  124. #define    kNumStaticContextStackEntries 5
  125.  
  126. struct ContextStack
  127. {
  128.     unsigned short    numEntries;
  129.     OSLContext            contextArray[kNumStaticContextStackEntries];
  130.     unsigned short    bufferSize;
  131.     OSLContext*            buffer;
  132. };
  133. typedef struct ContextStack ContextStack;
  134.  
  135. ContextStack    gContextStack =
  136.     {false, {{NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}}, 0, NULL};
  137.  
  138. //------------------------------------------------------------------------------
  139. // AddContextToTopOfStack
  140. //
  141. //    Use static entries in structure until they are filled up and then do
  142. //    dynamic allocation. We don't ever shrink the dynamic structure, we only
  143. //    grow it.
  144. //------------------------------------------------------------------------------
  145.  
  146. OSErr AddContextToTopOfStack(OSLContext* context)
  147. {
  148.     OSErr    error = noErr;
  149.  
  150. #if ODDEBUG
  151.     // WE ALWAYS HAVE AT LEAST ONE THAT'S PUT ON THE STACK BY iAEObjectInit
  152.     if (gContextStack.numEntries == 2)
  153.         DebugStr("\pSomeone is actually calling Resolve reentrantly!");
  154. #endif
  155.  
  156.     if (gContextStack.numEntries < kNumStaticContextStackEntries)
  157.         gContextStack.contextArray[gContextStack.numEntries] = *context;
  158.     else
  159.     {
  160.         // THE FIRST TIME WE ARE CALLED TO ALLOCATE ONE MORE CONTEXT THAN
  161.         //    kNumStaticContextStackEntries
  162.         if (gContextStack.buffer == NULL)
  163.         {
  164.             gContextStack.buffer = (OSLContext*)NewPtr(sizeof(OSLContext));
  165.             if (error == noErr)
  166.             {
  167.                 if (gContextStack.buffer == NULL)
  168.                     error = memFullErr;
  169.             }
  170.             if (error == noErr)
  171.                 gContextStack.bufferSize = sizeof(OSLContext);
  172.         }
  173.         else
  174.         {
  175.             Size  newBufferSize =
  176.                     (gContextStack.numEntries - kNumStaticContextStackEntries
  177.                         + 1) * sizeof(OSLContext);
  178.             if (gContextStack.bufferSize < newBufferSize)
  179.             {
  180.                 OSLContext*    newBuffer = (OSLContext*)NewPtr(newBufferSize);
  181.                 error = MemError();
  182.                 if (error == noErr)
  183.                 {
  184.                     BlockMoveData(gContextStack.buffer, newBuffer,
  185.                                     gContextStack.bufferSize);
  186.                     DisposePtr((Ptr)gContextStack.buffer);
  187.                     gContextStack.buffer = newBuffer;
  188.                     gContextStack.bufferSize = newBufferSize;
  189.                 }
  190.             }
  191.         }
  192.         if (error == noErr)
  193.             *(gContextStack.buffer
  194.                 + (gContextStack.numEntries - kNumStaticContextStackEntries)) = *context;
  195.     }
  196.  
  197.     if (error == noErr)
  198.         ++gContextStack.numEntries;
  199.  
  200.     return error;
  201. }
  202.  
  203. //------------------------------------------------------------------------------
  204. // RemoveTopOfContextStack
  205. //------------------------------------------------------------------------------
  206.  
  207. OSErr RemoveTopOfContextStack()
  208. {
  209.     --gContextStack.numEntries;
  210.     return noErr;
  211. }
  212.  
  213. //OSLContext    theGlobalContext;
  214.  
  215. //------------------------------------------------------------------------------
  216. // GetCurrentContext
  217. //------------------------------------------------------------------------------
  218.  
  219. OSErr GetCurrentContext( OSLContext* curContext )
  220. {
  221. //    *curContext = theGlobalContext;
  222.     if (gContextStack.numEntries <= kNumStaticContextStackEntries)
  223.         *curContext = gContextStack.contextArray[gContextStack.numEntries - 1];
  224.     else
  225.     {
  226.         *curContext =
  227.             *(gContextStack.buffer
  228.             + (gContextStack.numEntries - kNumStaticContextStackEntries - 1));
  229.     }
  230.     return noErr;
  231. }
  232.  
  233. //------------------------------------------------------------------------------
  234. // SetCurrentContext
  235. //------------------------------------------------------------------------------
  236.  
  237. OSErr SetCurrentContext( OSLContext* curContext )
  238. {
  239. //    theGlobalContext = *curContext;
  240.     if (gContextStack.numEntries <= kNumStaticContextStackEntries)
  241.         gContextStack.contextArray[gContextStack.numEntries - 1] = *curContext;
  242.     else
  243.     {
  244.         *(gContextStack.buffer
  245.             + (gContextStack.numEntries - kNumStaticContextStackEntries - 1))
  246.                     = *curContext;
  247.     }
  248.     return noErr;
  249. }
  250.  
  251.